package com.engine4cloud.tdf.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.admin.api.dto.AreaCreateDTO;
import com.engine4cloud.tdf.admin.api.dto.AreaQueryDTO;
import com.engine4cloud.tdf.admin.api.dto.AreaUpdateDTO;
import com.engine4cloud.tdf.admin.api.vo.AreaResultVO;
import com.engine4cloud.tdf.admin.service.SysAreaService;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 行政区划
 *
 * @author lbw
 * @date 2024-02-16 22:40:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysArea")
@Tag(description = "sysArea", name = "行政区划管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysAreaController {

    private final SysAreaService sysAreaService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return R<?>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @PostMapping("/page")
    public R<PageInfo<AreaResultVO>> getFirstFloorChildren(@ParameterObject Params page, @Valid @RequestBody AreaQueryDTO dto) {
        return R.ok(sysAreaService.pageList(page, dto));
    }


    /**
     * 行政区划详情
     *
     * @param dto 查询条件
     * @return R
     */
    @Inner
    @Operation(summary = "条件行政区划详情", description = "行政区划详情")
    @PostMapping("/detail")
    public R<AreaResultVO> getAreaDetail(@Valid @RequestBody AreaQueryDTO dto) {
        return R.ok(sysAreaService.getAreaDetail(dto));
    }

    /**
     * 通过id查询行政区划
     *
     * @param id 地区id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}/details")
    public R<AreaResultVO> getDetails(@PathVariable("id") Long id) {
        return R.ok(sysAreaService.findById(id));
    }

    /**
     * 新增行政区划
     *
     * @param dto 行政区划
     * @return R
     */
    @Operation(summary = "新增行政区划", description = "新增行政区划")
    @SysLog("新增行政区划")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_sysArea_add')")
    public R<Boolean> create(@RequestBody AreaCreateDTO dto) {
        return R.ok(sysAreaService.create(dto));
    }

    /**
     * 修改行政区划
     *
     * @param dto 行政区划
     * @return R
     */
    @Operation(summary = "修改行政区划", description = "修改行政区划")
    @SysLog("修改行政区划")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_sysArea_edit')")
    public R<Boolean> update(@RequestBody AreaUpdateDTO dto) {
        return R.ok(sysAreaService.update(dto));
    }

    /**
     * 通过id删除行政区划
     *
     * @param id id列表
     * @return R
     */
    @Operation(summary = "通过id删除行政区划", description = "通过id删除行政区划")
    @SysLog("通过id删除行政区划")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_sysArea_del')")
    public R<Boolean> removeById(@PathVariable("id") Long id) {
        return R.ok(sysAreaService.remove(id));
    }
}
